package com.jsy.common.util.secret;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class CustomEncryption {
    private static final int SHIFT = 3;

    public static byte[] encrypt(byte[] data) {
        byte[] encrypted = new byte[data.length];
        for (int i = 0; i < data.length; i++) {
            encrypted[i] = (byte) (data[i] + SHIFT);
        }
        return encrypted;
    }

    public static byte[] decrypt(byte[] encryptedData) {
        byte[] decrypted = new byte[encryptedData.length];
        for (int i = 0; i < encryptedData.length; i++) {
            decrypted[i] = (byte) (encryptedData[i] - SHIFT);
        }
        return decrypted;
    }

    public static void main(String[] args) {
        try {
            // 读取 .ftl 文件
            Path path = Paths.get("example.ftl");
            byte[] fileContent = Files.readAllBytes(path);

            // 加密文件内容
            byte[] encryptedContent = encrypt(fileContent);

            // 将加密后的内容写入新文件
            Path encryptedPath = Paths.get("example_encrypted.ftl");
            Files.write(encryptedPath, encryptedContent);
            System.out.println("File encrypted successfully.");

            // 解密文件内容
            byte[] decryptedContent = decrypt(encryptedContent);

            // 将解密后的内容写入新文件
            Path decryptedPath = Paths.get("example_decrypted.ftl");
            Files.write(decryptedPath, decryptedContent);
            System.out.println("File decrypted successfully.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}